package control;

import java.awt.event.KeyEvent;
import java.io.IOException;
import java.util.Random;

import model.FileVO;
import model.GenericTreeNode;

public class KeyboardManager {

	private Boolean controlKey = false;
	private Boolean upKey = false;
	private Boolean downKey = false;
	private Boolean enterKey = false;
	private Boolean leftKey = false;
	private Boolean cKey = false;
	private Boolean xKey = false;
	private Boolean rKey = false;
	private Boolean deleteKey = false;

	private Random random;

	public KeyboardManager(Random random) {
		this.random = random;
	}
	
	public void checkState(DirectoriesManager dm, DisplayManager dim,
			FileManager fm) {

		
		if (cKey) {
			copyFiles(dm, dim, fm);
			rKey = true;
		}

		if (xKey) {
			cutFiles(dm, dim, fm);
			rKey = true;
		}

		if (deleteKey) {
			deleteFiles(dm, dim, fm);
			rKey = true;
		}

		if(rKey) {
			rKey = false;
			leftKey = true;
			enterKey = true;
		}		
		
		if (upKey) {
			nextFile(dm, dim, fm);
		}

		if (downKey) {
			previousFile(dm, dim, fm);
		}

		if (leftKey) {
			getBackFromDirectory(dm, dim, fm);
		}

		if (enterKey) {
			getIntoDirectory(dm, dim, fm);
		}

		if (controlKey) {
			markFile(dm, dim, fm);
		}
	}

	public void keyPressed(KeyEvent arg0) {
		switch (arg0.getKeyCode()) {
		case KeyEvent.VK_UP:
			upKey = true;
			break;
		case KeyEvent.VK_DOWN:
			downKey = true;
			break;
		case KeyEvent.VK_LEFT:
			leftKey = true;
			break;
		case KeyEvent.VK_ENTER:
			enterKey = true;
			break;
		case KeyEvent.VK_CONTROL:
			controlKey = true;
			break;
		case KeyEvent.VK_C:
			cKey = true;
			break;
		case KeyEvent.VK_X:
			xKey = true;
			break;
		case KeyEvent.VK_R:
			rKey = true;
			break;
		case KeyEvent.VK_DELETE:
			deleteKey = true;
			break;
		default:
			break;
		}
	}

	public void keyReleased(KeyEvent arg0) {
		switch (arg0.getKeyCode()) {
		case KeyEvent.VK_UP:
			upKey = false;
			break;
		case KeyEvent.VK_DOWN:
			downKey = false;
			break;
		case KeyEvent.VK_LEFT:
			leftKey = false;
			break;
		case KeyEvent.VK_ENTER:
			enterKey = false;
			break;
		case KeyEvent.VK_CONTROL:
			controlKey = false;
			break;
		case KeyEvent.VK_C:
			cKey = false;
			break;
		case KeyEvent.VK_X:
			xKey = false;
			break;
		case KeyEvent.VK_R:
			rKey = false;
			break;
		case KeyEvent.VK_DELETE:
			deleteKey = false;
			break;
		default:
			break;
		}
	}
	
	
	public void copyFiles(DirectoriesManager dm, DisplayManager dim,
			FileManager fm) {
		try {
			if (!dm.getTargetedFile().getData().isDirectory()) {
				fm.copyFiles(dm.getMultiSelectList(), dm.getTargetedFile()
						.getParentNode());
			} else {
				fm.copyFiles(dm.getMultiSelectList(), dm.getTargetedFile());
			}
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			cKey = false;
			dm.getMultiSelectList().clear();
		}
	}
	
	
	public void cutFiles(DirectoriesManager dm, DisplayManager dim,
			FileManager fm) {
		try {
			if (!dm.getTargetedFile().getData().isDirectory()) {
				fm.cutFiles(dm.getMultiSelectList(), dm.getTargetedFile()
						.getParentNode());
			} else {
				fm.cutFiles(dm.getMultiSelectList(), dm.getTargetedFile());
			}
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			xKey = false;
			dm.getMultiSelectList().clear();
		}
	}
	
	public void deleteFiles(DirectoriesManager dm, DisplayManager dim,
			FileManager fm) {
		fm.removeFiles(dm.getMultiSelectList());
		deleteKey = false;
		dm.getMultiSelectList().clear();

	}
	
	private void markFile(DirectoriesManager dm, DisplayManager dim,
			FileManager fm) {
		if (!dm.getTargetedFile().getParentNode().getIsRoot()) {
			if ((!dm.getMultiSelectList().isEmpty())
					&& (dm.getMultiSelectList().get(0).getParentNode() == dm
							.getTargetedFile().getParentNode())) {

				if (dm.getMultiSelectList().contains(dm.getTargetedFile())) {
					dm.getMultiSelectList().remove(dm.getTargetedFile());
				} else {
					dm.getMultiSelectList().add(dm.getTargetedFile());
				}

				for (GenericTreeNode<FileVO> file : dm.getMultiSelectList()) {
					System.out.println(file.getData().getName());
				}
			} else {
				dm.getMultiSelectList().clear();
				dm.getMultiSelectList().add(dm.getTargetedFile());
			}
		}
		controlKey = false;
		
	}

	private void getIntoDirectory(DirectoriesManager dm, DisplayManager dim,
			FileManager fm) {
		if (!dm.getTargetedFile().getData().isDirectory()) {
			try {
				fm.openFile(dm.getTargetedFile().getData()
						.getAbsoluteFile());
			} catch (IOException e) {
				e.printStackTrace();
			}
		} else {
			if (!dm.getTargetedFile().hasChildren()) {
				if (dm.getTargetedFile().getData().isDirectory()) {
					dm.removeOtherChildElements(dm.getCurrentDirectory());

					float x, y, z;
					x = random.nextInt(3) - random.nextInt(3);
					y = random.nextInt(3) - random.nextInt(3);
					z = 7;
					dm.getTargetedFile().prepareChildElementsForFileVO(
							dm.getTargetedFile().getData().getPosX() + x,
							dm.getTargetedFile().getData().getPosY() + y,
							dm.getTargetedFile().getData().getPosZ() + z,
							0.75f);

					if (dm.getTargetedFile().getData().hasChildElements()) {
						dm.setTargetedFile(dm.getTargetedFile().getChildAt(
								0));
					}
				}

				if (dm.getTargetedFile().hasChildren()) {
					dm.setCurrentDirectory(dm.getTargetedFile()
							.getChildren());
					dm.setTargetedFile(dm.getTargetedFile().getChildAt(0));
				}

				dim.setDistance3(dm.getTargetedFile().getData().getPosZ() + 3.75f);

			}

		}
		enterKey = false;
		
	}

	private void getBackFromDirectory(DirectoriesManager dm,
			DisplayManager dim, FileManager fm) {
		if (!dm.getTargetedFile().getParentNode().getIsRoot()) {
			if (dm.getTargetedFile().getParentNode().hasParentElement()) {
				dm.setCurrentDirectory(dm.getTargetedFile().getParentNode()
						.getParentNode().getChildren());
				dm.setTargetedFile(dm.getTargetedFile().getParentNode());
				dm.getTargetedFile().removeChildren();
			} else {
				dm.setCurrentDirectory(dm.getDirectoriesTree()
						.getChildren());
				dm.setTargetedFile(dm.getTargetedFile().getParentNode());
				dm.getTargetedFile().removeChildren();
			}

			dim.setDistance3(dm.getTargetedFile().getData().getPosZ() + 3.75f);

		}
		leftKey = false;
		
	}

	private void previousFile(DirectoriesManager dm, DisplayManager dim,
			FileManager fm) {
		int targetId = dm.getCurrentDirectory().indexOf(
				dm.getTargetedFile());
		if (targetId > 0) {
			dm.setTargetedFile(dm.getCurrentDirectory().get(targetId - 1));

		}
		downKey = false;
		
	}

	private void nextFile(DirectoriesManager dm, DisplayManager dim,
			FileManager fm) {
		int targetId = dm.getCurrentDirectory().indexOf(
				dm.getTargetedFile());

		if (targetId < dm.getCurrentDirectory().size() - 1) {
			dm.setTargetedFile(dm.getCurrentDirectory().get(targetId + 1));
		}
		upKey = false;
		
	}
	
	

	

}
